import { NestFactory } from '@nestjs/core';
import { MicroEmailModule } from './micro-email.module';
import {join} from "path";
import {Transport} from "@nestjs/microservices";
import * as process from "process";
import { HttpAppModule } from './app-http.module';
import { ConfigModule } from '@nestjs/config';

async function bootstrap() {
  try {
    // 启动 HTTP 健康检查服务
    const appHttp = await NestFactory.create(HttpAppModule);
    await appHttp.listen(3006, '0.0.0.0'); // 移除回调，用 await 确保启动完成
    console.log(`✅ HTTP 健康检查服务启动：http://0.0.0.0:3006`);
    console.log('REDIS_PASSWORD:', process.env.REDIS_PASSWORD);
    // 启动 gRPC 服务
    const appGrpc = await NestFactory.createMicroservice(MicroEmailModule, {
      transport: Transport.GRPC,
      options: {
        package: 'email',
        protoPath: join(process.cwd(), 'proto/email/email.proto'), // 修正路径
        url: '0.0.0.0:50056',
      },
    });
    await appGrpc.listen();
    console.log(`✅ gRPC 服务启动：0.0.0.0:50056`);
  } catch (error) {
    console.error('❌ 服务启动失败：', error); // 捕获并打印所有启动错误
    process.exit(1); // 启动失败时退出进程
  }
}

bootstrap();
